package com.haiyu.manager.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.haiyu.manager.dao.*;
import com.haiyu.manager.dto.OrderDTO;
import com.haiyu.manager.dto.OrderDetailDTO;
import com.haiyu.manager.dto.OrderSearchDTO;
import com.haiyu.manager.dto.UserDTO;
import com.haiyu.manager.pojo.Order;
import com.haiyu.manager.pojo.Send;
import com.haiyu.manager.response.PageDataResult;
import com.haiyu.manager.service.OrderService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
@Service
public class OrderServiceImpl implements OrderService {
    @Autowired
    OrderMapper orderMapper;
    @Autowired
    OrderDetailMapper orderDetailMapper;
    @Autowired
    UserInfoMapper userInfoMapper;
    @Autowired
    SendMapper sendMapper;
    @Autowired
    UserAddressInfoMapper userAddressInfoMapper;
    private Logger logger = LoggerFactory.getLogger(this.getClass());

    @Override
    public PageDataResult getOrderList(OrderSearchDTO orderSearch, Integer pageNum, Integer pageSize) {
        PageDataResult pageDataResult = new PageDataResult();
        List<OrderDTO> orderDTOS = orderMapper.getOrderList(orderSearch);
        for(int i=0;i<orderDTOS.size(); i++)    {
            OrderDetailDTO orderDetailDTO = orderDetailMapper.getOrderDetailDTO(orderDTOS.get(i).getDetailId());
            UserDTO userDTO=userInfoMapper.getUserNickname(orderDTOS.get(i).getAddressId());
            //orderDTOS.get(i).setOrderDetailDTO(orderDetailDTO);
            orderDTOS.get(i).setCategory2Id(orderDetailDTO.getCategory2Id());
            orderDTOS.get(i).setCategory2Name(orderDetailDTO.getCategory2Name());
            orderDTOS.get(i).setUserNickname(userDTO.getUserNickname());
        }
        System.out.println("orderSearch有" + orderSearch);
        System.out.println("orderDTOS有" + orderDTOS);
        PageHelper.startPage(pageNum, pageSize);

        if (orderDTOS.size() != 0) {
            PageInfo<OrderDTO> pageInfo = new PageInfo<>(orderDTOS);
            pageDataResult.setList(orderDTOS);
            pageDataResult.setTotals((int) pageInfo.getTotal());
        }
        return pageDataResult;
    }

    @Override
    public Map<String, Object> updateOrder(Order order) {
        Map<String,Object> data = new HashMap();

        int result = orderMapper.updateOrder(order);
        /////////////
        //插入信息
        OrderSearchDTO orderSearch = new OrderSearchDTO();
        orderSearch.setOrderId(order.getOrderId());
        if(result==1) {
            Send send = new Send();
            send.setOrderId(order.getOrderId());
            send.setMessageId(4);
            send.setBuyerOpenid(orderMapper.getOrderList(orderSearch).get(0).getBuyerOpenid());

            System.out.println(send);
            int flag3 = sendMapper.insert(send);
            if (flag3==1) {
                System.out.println("插入数据库send失败！");

            } else {
                System.out.println("插入数据库send成功！");
            }
        }


        /////////////

        if(result == 0){
            data.put("code",0);
            data.put("msg","回复失败！");
            logger.error("board[更新]，结果=更新失败！");
            return data;
        }
        data.put("code",1);
        data.put("msg","回复成功！");
        logger.info("board[更新]，结果=更新成功！");
        return data;
    }

    @Override
    public Order getOrderById(Integer orderId) {
        return orderMapper.selectByPrimaryKey(orderId);
    }
}
